import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt


def get_testtype(testtype_path,testtype_png_path):
    import pandas as pd
    import matplotlib.pyplot as plt

    df = pd.read_pickle(testtype_path)
    df['fail_case_cnt'] = df['fail_case_cnt'].astype(int)
    df['passed_case_cnt'] = df['passed_case_cnt'].astype(int)

    # 创建一个数字序列，作为 x 轴的位置
    x_positions = range(len(df['test_type']))

    plt.figure(figsize=(10, 6))
    plt.rcParams['font.sans-serif'] = ["SimHei"]

    # 使用 align='center' 参数，使得柱状图居中显示在给定的位置上
    plt.bar([x - 0.2 for x in x_positions], df['fail_case_cnt'], width=0.05, label='失败次数', color='red', align='center')
    plt.bar([x + 0.2 for x in x_positions], df['passed_case_cnt'], width=0.05, label='通过次数', color='green',
            align='center')

    plt.xlabel('测试类型')
    plt.ylabel('次数')
    plt.title('不同测试类型对应的成功次数和失败次数')
    plt.legend()

    # 设置 x 轴刻度标签
    plt.xticks(x_positions, df['test_type'])

    # 保存图表为图像文件
    plt.savefig(testtype_png_path, format='png')
    plt.close()

def get_module(module_path,module_png_path):
    import pandas as pd
    import matplotlib.pyplot as plt

    df = pd.read_pickle(module_path)
    df['fail_case_cnt'] = df['fail_case_cnt'].astype(int)
    df['passed_case_cnt'] = df['passed_case_cnt'].astype(int)

    # 创建一个数字序列，作为 x 轴的位置
    x_positions = range(len(df['module']))

    plt.figure(figsize=(10, 6))
    plt.rcParams['font.sans-serif'] = ["SimHei"]

    # 使用 align='center' 参数，使得柱状图居中显示在给定的位置上
    plt.bar([x - 0.2 for x in x_positions], df['fail_case_cnt'], width=0.05, label='失败次数', color='red', align='center')
    plt.bar([x + 0.2 for x in x_positions], df['passed_case_cnt'], width=0.05, label='通过次数', color='green',
            align='center')

    plt.xlabel('模块')
    plt.ylabel('次数')
    plt.title('不同模块对应的成功次数和失败次数')
    plt.legend()

    # 设置 x 轴刻度标签
    plt.xticks(x_positions, df['module'])

    # 保存图表为图像文件
    plt.savefig(module_png_path, format='png')
    plt.close()

def get_total(total_path, total_png_path):
    df = pd.read_pickle(total_path)
    print(df.columns)
    df['fail_case_cnt'] = df['fail_case_cnt'].astype(int)
    df['passed_case_cnt'] = df['passed_case_cnt'].astype(int)
    print(df)
    # 计算总和
    total_fail = df['fail_case_cnt'].sum()
    total_pass = df['passed_case_cnt'].sum()

    # 计算占比
    fail_percent = (total_fail / (total_fail + total_pass)) * 100
    pass_percent = (total_pass / (total_fail + total_pass)) * 100

    print("fail_case_cnt:"+str(fail_percent))
    print("passed_case_cnt:" + str(pass_percent))

    # 创建标签
    labels = ['失败', '成功']
    # 创建占比列表
    sizes = [fail_percent, pass_percent]

    # 颜色
    colors = ['red', 'green']

    # 突出 Fail Cases 部分
    explode = (0.1, 0)  # 突出 Fail Cases 部分

    # 绘制饼状图
    plt.rcParams['font.sans-serif'] = ["SimHei"]
    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)

    # 添加标题
    plt.title('成功次数和失败次数分布')

    # 保存图像为PNG文件
    plt.savefig(total_png_path, format='png')

    # 显示图表
    plt.show()


def virsul_step1(pt_date,pt_hour,savepath):
    testtype_path=savepath + 'signal_testtype_' + pt_date+'_'+pt_hour+'.pkl'
    testtype_png_path = savepath + 'signal_testtype_' + pt_date + '_' + pt_hour + '.png'
    get_testtype(testtype_path, testtype_png_path)
    module_path=savepath + 'signal_module_' + pt_date+'_'+pt_hour+'.pkl'
    module_png_path = savepath + 'signal_module_' + pt_date + '_' + pt_hour + '.png'
    get_module(module_path, module_png_path)
    total_path=savepath + 'signal_total_' + pt_date+'_'+pt_hour+'.pkl'
    total_png_path = savepath + 'signal_total_' + pt_date + '_' + pt_hour + '.png'
    get_total(total_path, total_png_path)


if __name__ == '__main__':
    # 示例数据
    pt_date = '20231205'
    pt_hour = '19'
    savepath = '../csv/'
    virsul_step1(pt_date, pt_hour, savepath)




